package com.hdoit.ares.web.home.wechat;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.ares.logical.acti.service.IActiTaskService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.insu.model.Insurance;
import com.hdoit.ares.logical.insu.service.IInsuranceService;
import com.hdoit.ares.logical.official.model.Artical;
import com.hdoit.ares.logical.official.model.ArticalClassify;
import com.hdoit.ares.logical.official.service.IArticalClassifyService;
import com.hdoit.ares.logical.official.service.IArticalService;
import com.hdoit.ares.logical.viewmodel.insu.FindInsu;
import com.hdoit.ares.logical.viewmodel.official.ArticalClassifySearch;
import com.hdoit.ares.logical.viewmodel.official.ArticalSearch;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.web.home.wechat.BaseHomeWechat;

@Controller
@RequestMapping(value = "/wechat/home/")
public class HomeWechat extends BaseHomeWechat{

	@Override
	public String getRoutePath() {
		return "areas/wechat/home/";
	}
	
	private IInsuranceService insuranceService;
	private IArticalClassifyService articalClassifyService;
	private IArticalService articalService;
	private ICustomerService customerService;
	private IActiTaskService actiTaskService;
	
	public ICustomerService getCustomerService() {
		return customerService;
	}
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	public IInsuranceService getInsuranceService() {
		return insuranceService;
	}
	@Resource
	public void setInsuranceService(IInsuranceService insuranceService) {
		this.insuranceService = insuranceService;
	}	
	
	public IArticalClassifyService getArticalClassifyService() {
		return articalClassifyService;
	}
	@Resource
	public void setArticalClassifyService(IArticalClassifyService articalClassifyService) {
		this.articalClassifyService = articalClassifyService;
	}

	public IArticalService getArticalService() {
		return articalService;
	}
	@Resource
	public void setArticalService(IArticalService articalService) {
		this.articalService = articalService;
	}	

	public IActiTaskService getActiTaskService() {
		return actiTaskService;
	}
	@Resource
	public void setActiTaskService(IActiTaskService actiTaskService) {
		this.actiTaskService = actiTaskService;
	}

	private final String indexViewName="index";
	private final String channelViewName = "channel";
	

//	@WechatAuthPassport
	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String index(Model model){
		WechatLoginModel loginModel=wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		System.out.println("customer之前");		
		Customer customer=(Customer)loginModel.get(AresManageContainer.customer_account_info);
		System.out.println("customer之后");
		
		FindInsu findInsu = new FindInsu();
		findInsu.setStatus("hotInsurance");
		findInsu.setOrder("hotSortCode");
		findInsu.setUpOrDown("asc");
				
		List<Insurance> insurances = insuranceService.queryListByFindInsu(findInsu);	
		System.out.println("insurances之后");
		model.addAttribute("insurances", insurances);
		
		
		model.addAttribute("customer", customer);	
		
		/*String key = ZzbConfiguration.getInstance().getValue(ZzbManageContainer.zhongan_openKey);
		String data = "{\"userID\":\""+customer.getId()+"\"}";
		String sign = RC4Util.encryRC4String(data, key);
		String bizContent = "bizContent="+sign;
		model.addAttribute("bizContent", bizContent);*/
		
		FindInsu findInsu1 = new FindInsu();
		findInsu1.setStatus("chooseProduct1");
		List<Insurance> product1 = insuranceService.queryListByFindInsu(findInsu1);
		if(product1.size()>0){
			Insurance chooseProduct1=product1.get(0);
			model.addAttribute("chooseProduct1",chooseProduct1);
		}else{
			System.err.println("精选产品1为空");
		}		
		
		FindInsu findInsu2 = new FindInsu();
		findInsu2.setStatus("chooseProduct2");
		List<Insurance> product2 = insuranceService.queryListByFindInsu(findInsu2);
		if(product2.size()>0){
			Insurance chooseProduct2=product2.get(0);
			model.addAttribute("chooseProduct2",chooseProduct2);
		}else{
			System.err.println("精选产品2为空");
		}
		
		FindInsu findInsu3 = new FindInsu();
		findInsu3.setStatus("chooseProduct3");
		List<Insurance> product3 = insuranceService.queryListByFindInsu(findInsu3);
		if(product3.size()>0){
			Insurance chooseProduct3=product3.get(0);
			model.addAttribute("chooseProduct3",chooseProduct3);
		}else{
			System.err.println("精选产品3为空");
		}
		
		FindInsu findInsu4 = new FindInsu();
		findInsu4.setStatus("chooseProduct4");
		List<Insurance> product4 = insuranceService.queryListByFindInsu(findInsu4);
		if(product4.size()>0){
			Insurance chooseProduct4=product4.get(0);
			model.addAttribute("chooseProduct4",chooseProduct4);
		}else{
			System.err.println("精选产品4为空");
		}
		
		//取出轮播资讯
		ArticalClassifySearch search = new ArticalClassifySearch();
		search.setFunctionURL("areas/wechat/share/information/shareInformation");
		ArticalClassify articalClassify = articalClassifyService.queryModel(search);
		model.addAttribute("articalClassify", articalClassify);
		
		ArticalSearch articalSearch = new ArticalSearch();
		articalSearch.setTypeId(articalClassify.getId());
		List<Artical> articals = articalService.queryList(search);
		model.addAttribute("articals", articals);
				
		//任务引导
		/*Calendar now = Calendar.getInstance();
		String day = now.get(Calendar.DAY_OF_MONTH)+"";
		String  month = (now.get(Calendar.MONTH)+1)+"";		
		String taskToGuide =  actiTaskService.isSigned(month, day, customer.getId());		
		model.addAttribute("taskToGuide", taskToGuide);	*/	
		
		return view(indexViewName);		
	}
	
	@RequestMapping(value = channelViewName, method = { RequestMethod.GET, RequestMethod.POST}, produces = "text/plain;charset=utf-8")
	public String channel(Model model,String channelCode){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer = customerService.queryModel(loginModel.getUserId());
		
		if(!StringUtils.isEmpty(channelCode)){
			Integer userIdentity = Integer.valueOf(channelCode);
			if(customer.getRefereeIdentity()<=0){
				Customer refreeCustomer = customerService.queryModelByUserIdentity(userIdentity);
				if(refreeCustomer!=null){
					customer.setRefereeId(refreeCustomer.getId());
					customer.setRefereeIdentity(userIdentity);
					customer.setModelStatus(ModelStatus.update);
					customerService.applyChange(customer);
				}
				
			}
		}
		FindInsu findInsu = new FindInsu();
		findInsu.setStatus("hotInsurance");
		findInsu.setOrder("hotSortCode");
		findInsu.setUpOrDown("asc");
				
		List<Insurance> insurances = insuranceService.queryListByFindInsu(findInsu);	
		model.addAttribute("insurances", insurances);
		model.addAttribute("customer", customer);	
		return view(indexViewName);
	}
	
}
